package com.bosssoft.controller;

import com.bosssoft.permission.centre.api.ResourceApi;
import com.bosssoft.permission.centre.pojo.dto.pcp.ResourceDTO;
import com.bosssoft.permission.centre.pojo.query.ResourceQuery;
import com.bosssoft.permission.centre.pojo.vo.ResourceVO;
import com.bosssoft.permission.centre.pojo.vo.SubSystemVO;
import com.bosssoft.permission.centre.service.ResourceService;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ResourceController implements ResourceApi {

    @Autowired
    private ResourceService resourceService;


    @ApiLog
    @Override
    public List<ResourceVO> commonQuery(ResourceQuery resourceQuery) {
        if (resourceQuery == null) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        List<ResourceDTO> resourceDTOS = resourceService.commonQuery(resourceQuery);


        if (resourceDTOS != null) {
            List<ResourceVO> resourceVOS = BeanUtil.copyList(resourceDTOS, ResourceVO.class, new BeanUtil.CopyCallback() {
                @Override
                public void copy(Object o, Object o1) {
                    ResourceDTO resourceDTO = (ResourceDTO) o;
                    ResourceVO resourceVO = (ResourceVO) o1;

                    if(resourceDTO.getResourceDTO()!=null){
                        ResourceVO copy = BeanUtil.copy(resourceDTO.getResourceDTO(), ResourceVO.class);
                        resourceVO.setResourceVO(copy);
                    }
                    if(resourceDTO.getSubSystem()!=null){
                        BeanUtil.copy(resourceDTO.getSubSystem(), resourceVO.getSubSystemVO(), SubSystemVO.class);
                    }
                }
            });
            return resourceVOS;
        } else return new ArrayList();
    }

    @ApiLog
    @Override
    public List<ResourceVO> query(ResourceQuery resourceQuery) {
        if (resourceQuery == null) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        List<ResourceDTO> resourceDTOS = resourceService.query(resourceQuery);
        List<ResourceVO> resourceVOS = BeanUtil.copyList(resourceDTOS, ResourceVO.class);
        return resourceVOS;
    }

    @ApiLog
    @Override
    public Boolean add(@Valid ResourceDTO resourceDTO) {
        java.util.Date date=new java.util.Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        resourceDTO.setCreatedTime(timestamp);
        resourceDTO.setUpdatedTime(timestamp);
        Boolean result = resourceService.add(resourceDTO);
        return result;
    }

    @Override
    public ResourceVO queryResourceById(Long id) {
        ResourceDTO resourceDTO = resourceService.queryById(id);
        if (resourceDTO == null) {
            return new ResourceVO();
        }
        ResourceVO resourceVO = BeanUtil.copy(resourceDTO, ResourceVO.class);
        if(resourceDTO.getResourceDTO()!=null){
            ResourceVO copy = BeanUtil.copy(resourceDTO.getResourceDTO(), ResourceVO.class);
            resourceVO.setResourceVO(copy);
        }

        if(resourceDTO.getSubSystem()!=null){
            SubSystemVO copy = BeanUtil.copy(resourceDTO.getSubSystem(), SubSystemVO.class);
            resourceVO.setSubSystemVO(copy);
        }

        return resourceVO;
    }

    @Override
    public ResourceVO queryResourceByParentId(Long id) {

        ResourceVO resourceVO = BeanUtil.copy(resourceService.findByParentId(id), ResourceVO.class);
        return resourceVO;
    }

    @Override
    public List<ResourceVO> queryResourceByMenu() {

        List<ResourceVO> resourceVOS = BeanUtil.copyList(resourceService.queryResourceByMenu(), ResourceVO.class);
       return resourceVOS;
    }

    @Override
    public Boolean remove(Long id) {
        return resourceService.remove(id);
    }

    @Override
    public Boolean batchDelete(@Valid ResourceDTO[] resourceDTOS) {
        return resourceService.batchDelete(resourceDTOS);
    }

    @Override
    public Boolean modify(@Valid ResourceDTO resourceDTO) {
        return resourceService.modify(resourceDTO);
    }

    @Override
    public ResourceDTO copy(Long id) {
        return resourceService.copy(id);
    }
}
